﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ConversationBoard : MonoBehaviour {

    class StorySequenceData
    {
        public int id = 0;
        public int sub_Id = 0;
        public string character = "teharil";
        public string character_Location = "left";
        public int emotion = 0;
        public string speech_body = "어쩌고 저쩌고 저쩌고 어쩌고";
        public string speech_string = "string_story_book.dialogue_1_1";
    }

    static public ConversationBoard instance;
    const string prefabPath = "Prefabs/ConversationBoard/ConversationBoard";

    static Vector3 s_OrizinScaleOfLeftPortrait;
    static Vector3 s_OrizinScaleOfRightPortrait;

    int m_Step = 0;
    int m_ID = 0;
    bool m_IsEndStep = false;
    bool m_IsTextAnimating = false;

    StorySequenceData m_SSData;

    public UITexture m_LeftUserPortrait;
    public UITexture m_RightUserPortrait;
    public UILabel m_LeftUserLabel;
    public UILabel m_RightUserLabel;

    const float typingTime = 0.05f;
    float m_TextAniTime = 0;
    int m_CrrCharIndex = 0;
    bool m_IsNeedAni = false;

    //테스크 코드
    static Dictionary<int, List<StorySequenceData>> datas = new Dictionary<int, List<StorySequenceData>>();

    static public void ShowConversation(int ID, bool isNeedAni)
    {
        if (instance == null)
        {
            // 테스트 코드
            System.Random rand = new System.Random();

            int sub1 = 0;
            int sub2 = 0;

            for (int i = 0; i < 10; i++)
            {
                StorySequenceData tempdata = new StorySequenceData();
                tempdata.id = rand.Next(0, 2);

                tempdata.id = 0;

                if (tempdata.id == 0)
                {
                    sub1++;
                    tempdata.sub_Id = sub1;
                }
                else
                {
                    sub2++;
                    tempdata.sub_Id = sub2;
                }

                if (rand.Next(0, 2) == 0)
                    tempdata.character_Location = "left";
                else
                    tempdata.character_Location = "right";

                tempdata.speech_body = string.Format("{0} 블라블라블라 {1} 길게 써야지 문자를 그래야 테스트를 해볼테니깐 안그래?", tempdata.id, tempdata.sub_Id);
                //datas.Add(tempdata.id, tempdata);

                if (!datas.ContainsKey(tempdata.id))
                {
                    List<StorySequenceData> tempList = new List<StorySequenceData>();
                    datas.Add(tempdata.id, tempList);
                }

                datas[tempdata.id].Add(tempdata);               
            }
            // 테스트 코드 끝

            GameObject prefab = Resources.Load(prefabPath) as GameObject;
            GameObject tempObj = Instantiate(prefab) as GameObject;
            GameObject UiRoot = GameObject.Find("UI Root (2D)/Camera/Anchor");

            tempObj.transform.parent = UiRoot.transform;
            tempObj.transform.localScale = Vector3.one;
            tempObj.transform.localPosition = Vector3.zero;

            instance = tempObj.GetComponent("ConversationBoard") as ConversationBoard;

            s_OrizinScaleOfLeftPortrait = instance.m_LeftUserPortrait.transform.localScale;
            s_OrizinScaleOfRightPortrait = instance.m_RightUserPortrait.transform.localScale;
        }

        instance.m_Step = 0;
        instance.m_ID = ID;
        instance.gameObject.SetActive(true);
        instance.m_IsNeedAni = isNeedAni;
        instance.ShowWithID();
    }

    void ShowWithID()
    {
        m_IsTextAnimating = m_IsNeedAni;
        m_TextAniTime = 0;
        m_CrrCharIndex = 0;

        if (datas.ContainsKey(m_ID))
        {
            if (datas[m_ID].Count > m_Step)
            {
                m_SSData = datas[m_ID][m_Step];

                if (m_SSData.character_Location.Equals("left"))
                {
                    m_LeftUserLabel.gameObject.SetActive(true);
                    m_RightUserLabel.gameObject.SetActive(false);

                    if(m_IsNeedAni)
                        m_LeftUserLabel.text = "";
                    else
                        m_LeftUserLabel.text = m_SSData.speech_body;


                    iTween.ScaleTo(m_LeftUserPortrait.gameObject, iTween.Hash("islocal", true, "scale", s_OrizinScaleOfLeftPortrait * 1.2f,
                        "looptype", "pingPong", "time", 0.5f, "easetype", "linear"));
                    iTween.Stop(m_RightUserPortrait.gameObject);
                    m_RightUserPortrait.transform.localScale = s_OrizinScaleOfRightPortrait;
                }
                else
                {
                    m_RightUserLabel.gameObject.SetActive(true);
                    m_LeftUserLabel.gameObject.SetActive(false);
                    
                    if(m_IsNeedAni)
                        m_RightUserLabel.text = "";
                    else
                        m_RightUserLabel.text = m_SSData.speech_body;

                    iTween.ScaleTo(m_RightUserPortrait.gameObject, iTween.Hash("islocal", true, "scale", s_OrizinScaleOfRightPortrait * 1.2f,
                        "looptype", "pingPong", "time", 0.5f, "easetype", "linear"));
                    iTween.Stop(m_LeftUserPortrait.gameObject);
                    m_LeftUserPortrait.transform.localScale = s_OrizinScaleOfLeftPortrait;
                }

                m_IsEndStep = false;
            }
            else // 해당 아이디 스토리 끝
            {
                m_IsEndStep = true;
                HideConversation();
            }
        }
        else
            Debug.LogError("Not Exist stoy sequence ID!");
    }

    void Update()
    {
        if (m_IsTextAnimating)
        {
            m_TextAniTime += Time.deltaTime;
            if (m_TextAniTime > typingTime)
            {
                m_TextAniTime = 0;
                m_CrrCharIndex++;

                if (m_CrrCharIndex >= m_SSData.speech_body.Length)
                {
                    if (m_SSData.character_Location.Equals("left"))
                        m_LeftUserLabel.text = m_SSData.speech_body;
                    else
                        m_RightUserLabel.text = m_SSData.speech_body;

                    m_IsTextAnimating = false;
                }
                else
                {
                    if (m_SSData.character_Location.Equals("left"))                    
                        m_LeftUserLabel.text = m_SSData.speech_body.Remove(m_CrrCharIndex);                    
                    else
                        m_RightUserLabel.text = m_SSData.speech_body.Remove(m_CrrCharIndex);
                }
            }
        }
    }

    void HideConversation()
    {
        instance.gameObject.SetActive(false);
        iTween.Stop(m_LeftUserPortrait.gameObject);
        iTween.Stop(m_RightUserPortrait.gameObject);
    }

    void OnBG(GameObject obj)
    {
        if (m_IsTextAnimating)
        {
            if (m_SSData.character_Location.Equals("left"))
                m_LeftUserLabel.text = m_SSData.speech_body;
            else
                m_RightUserLabel.text = m_SSData.speech_body;

            m_IsTextAnimating = false;
        }
        else
        {
            m_Step++;
            ShowWithID();
        }
    }

    void OnSkip(GameObject obj)
    {
        HideConversation();
    }
}
